//
//  HomeViewController.m
//  TY-C
//
//  Created by xyyl on 2024/2/29.
//

#import "HomeViewController.h"
#import "Detail_LineBtn.h"
#import "ZyqScrollerView.h"
#import "Home_PtViewController.h"
#import "Home_HbViewController.h"
#import "Pac_TipsSureBtn.h"
#import "ComonFile.h"

@interface HomeViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) Detail_LineBtn * lineBtn1;
@property (nonatomic,strong) Detail_LineBtn * lineBtn2;

@property (nonatomic,strong) ZyqScrollerView *bigScrollerView;
@property (nonatomic,assign) CGFloat scrollViewContent;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _scrollViewContent = 0;

    [self initTopBtn];
    [self initScrollerView];
    [self addChildVC];


    
}

- (void)addChildVC {
    Home_PtViewController * hh = [[Home_PtViewController alloc]init];
    [self addChildViewController:hh];
    
    Home_HbViewController * hh2 = [[Home_HbViewController alloc]init];
    [self addChildViewController:hh2];
    
    AiBaseViewController * vc = self.childViewControllers[0];
    
    vc.viewWidth = CGRectGetMaxX(_bigScrollerView.frame);
    
    vc.view.frame = CGRectMake((int)_scrollViewContent * CGRectGetWidth(_bigScrollerView.frame), 0,CGRectGetWidth(_bigScrollerView.frame), CGRectGetHeight(_bigScrollerView.frame));
    [_bigScrollerView addSubview:vc.view];
    _bigScrollerView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame) * 2, 0);

}

- (void)initScrollerView {
    _bigScrollerView = [[ZyqScrollerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_lineBtn1.frame), self.viewWidth, CGRectGetHeight(self.view.frame)- CGRectGetMaxY(_lineBtn1.frame))];
    [self.view addSubview:_bigScrollerView];
    _bigScrollerView.delegate = self;
    _bigScrollerView.showsVerticalScrollIndicator = NO;
    _bigScrollerView.showsHorizontalScrollIndicator =NO;
    _bigScrollerView.pagingEnabled = YES;
    _bigScrollerView.scrollEnabled = NO;
//    NSLog(@"%@",self.view.frame);
}

- (void)initTopBtn {
    _lineBtn1 = [[Detail_LineBtn alloc]initWithFrame:CGRectMake(25*IPADSCALE, 18*IPADSCALE, 70*IPADSCALE, 21*IPADSCALE)];
    _lineBtn1.selected = YES;
    _lineBtn1.nameLab.text = @"普通肌电检测";
    [self.view addSubview:_lineBtn1];
    
    _lineBtn2 = [[Detail_LineBtn alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_lineBtn1.frame), CGRectGetMinY(_lineBtn1.frame), 70*IPADSCALE, 21*IPADSCALE)];
    _lineBtn2.selected = NO;
    _lineBtn2.nameLab.text = @"喉部肌电检测";
    [self.view addSubview:_lineBtn2];

    [_lineBtn1 addTarget:self action:@selector(lineBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_lineBtn2 addTarget:self action:@selector(lineBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _lineBtn1.tag = 0;
    _lineBtn2.tag = 1;
    
}

- (void)lineBtnAction:(UIButton *)btn {
    
//    Pac_TipsSureBtn * ppp = [[Pac_TipsSureBtn alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
//    [self.view addSubview:ppp];
//
//    [ppp mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo(@(225 * IPADSCALE));
//        make.height.equalTo(@(160* IPADSCALE));
//        make.center.equalTo(self.view);
//    }];
//    [ppp sureBtnwithDoneBlock:^(Pac_TipsSureBtn * _Nonnull pickerBtn, NSInteger selectedIndex, id  _Nonnull selectedValue) {
//        NSLog(@"%@,%ld",self,selectedIndex);
//    } CancelBlock:^(Pac_TipsSureBtn * _Nonnull pickerBtn) {
//
//    }];
//    return;;
    
    _lineBtn1.selected = NO;
    _lineBtn2.selected = NO;
    btn.selected = YES;
    [_bigScrollerView setContentOffset:CGPointMake(CGRectGetWidth(self.view.frame) * btn.tag, 0) animated:NO];
    _scrollViewContent = btn.tag;
    [self scrollViewShowViewController];
}

- (void)scrollViewShowViewController {
    NSArray * array = @[_lineBtn1,_lineBtn2];
    int i = (int)_scrollViewContent;
    if (i >= array.count) {
        return;
    }
    UIButton * btn = array[i];
    btn.selected = YES;
    
    
    AiBaseViewController * vc = self.childViewControllers[i];
    vc.viewWidth = CGRectGetMaxX(_bigScrollerView.frame);

    if (vc.view.superview != nil) {
        return;
    }

    vc.view.frame = CGRectMake((int)_scrollViewContent * (CGRectGetWidth(_bigScrollerView.frame)), 0, CGRectGetWidth(_bigScrollerView.frame), CGRectGetHeight(_bigScrollerView.frame));
    [_bigScrollerView addSubview:vc.view];
}





@end
